package adv7.bank;

import java.util.Random;

/**
 * @author oldliu
 * @since 1.0
 */
public class Taker extends Thread{
    private Account ac;
    public Taker(Account ac,String name){
        super(name);
        this.ac=ac;
    }

    @Override
    public  void run() {
        Random r=new Random();
        while(ac.getMoney()>0){
            int m=r.nextInt(200);
            System.out.println(super.getName()+"准备取钱"+m+"元，当前余额是："+ac.getMoney()+"元");
            synchronized (ac){
                if(ac.getMoney()>=m){
                    try {
                        sleep(15);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ac.withdraw(m);
                }
            }
            System.out.println(super.getName()+"取钱"+m+"元，余额是："+ac.getMoney()+"元");
        }
    }
}
